//
//  HLTUpdatecardpasswordTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/9/6.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTUpdatecardpasswordTableViewController.h"
@interface HLTUpdatecardpasswordTableViewController ()

@property (weak, nonatomic) IBOutlet UILabel *cardno;


@property (weak, nonatomic) IBOutlet UILabel *oldpwd;

@property (weak, nonatomic) IBOutlet UILabel *newpwd;


@property (weak, nonatomic) IBOutlet UILabel *aginnewpwd;




@property NSString *oldpwdstr;
@property NSString *newpwdstr;

@property NSString *aginnewpwdstr;






@end

@implementation HLTUpdatecardpasswordTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    
    self.cardno.text=acc.cardNo;
    
    self.oldpwdstr=@"";
    self.newpwdstr=@"";
    self.aginnewpwdstr=@"";
    
    [self.navigationItem setTitle:@"卡密码修改"];
    
    
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (indexPath.row==1)
        {
            [EYInputPopupView popViewWithTitlenumber:@"填写原密码" contentText:self.oldpwd.text
                                          type:EYInputPopupView_Type_single_line_text
                                   cancelBlock:^{
                                       
                                   } confirmBlock:^(UIView *view, NSString *text) {
                                       
                                       self.oldpwd.text=@"******";
                                       self.oldpwdstr=text;
                                       
                                   } dismissBlock:^{
                                       
                                   }];
        }else if (indexPath.row==2)
        {
            [EYInputPopupView popViewWithTitlenumber:@"设置新密码(限制为6位)" contentText:self.newpwd.text
                                          type:EYInputPopupView_Type_single_line_text
                                   cancelBlock:^{
                                       
                                   } confirmBlock:^(UIView *view, NSString *text) {
                                       
                                       self.newpwd.text=@"******";
                                       self.newpwdstr=text;
                                       
                                       
                                   } dismissBlock:^{
                                       
                                   }];
        }else if (indexPath.row==3)
        {
            [EYInputPopupView popViewWithTitlenumber:@"确认密码" contentText:self.aginnewpwd.text
                                          type:EYInputPopupView_Type_single_line_text
                                   cancelBlock:^{
                                       
                                   } confirmBlock:^(UIView *view, NSString *text) {
                                        self.aginnewpwd.text=@"******";
                                       self.aginnewpwdstr=text;
                                   } dismissBlock:^{
                                       
                                   }];
        }
        
        
        
        
        
    }
    
    
    
    
    
}







- (IBAction)tijiao:(id)sender {
    
    
    
    if ([self.oldpwdstr isEqual:@"原密码"]) {
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请输入原密码"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
        
    }else if ([self.oldpwdstr length]!=6)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"原密码长度必须6位"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
    }else if ([self.newpwdstr length]!=6)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"新密码长度必须6位"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
    }else if (![self.newpwdstr isEqual:self.oldpwdstr])
    {
        if (![self.newpwdstr isEqual:self.aginnewpwdstr]) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"新密码和确认密码必须相同"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            [alert show];
            
        }
        else
        {
            if ([self.newpwdstr isEqual:@"新密码"]) {
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"必须设置新密码"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                [alert show];
            }
            else{
                
                UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
                view1.backgroundColor=[UIColor grayColor];
                view1.alpha=0.4;
                [self.view addSubview:view1];
                MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
                indicatorView.delegate = self;
                indicatorView.numberOfCircles = 3;
                indicatorView.radius = 20;
                indicatorView.internalSpacing = 3;
                indicatorView.center = self.view.center;
                [indicatorView startAnimating];
                [self.view addSubview:indicatorView];
                
                
                Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                
                //base64 编码
                NSString *base64oldpwd= [SetBase64andMd5 base64:self.oldpwdstr ncryptOrDecrypt:kCCEncrypt];
                NSString *base64newpwdstr= [SetBase64andMd5 base64:self.newpwdstr ncryptOrDecrypt:kCCEncrypt];
                
                
                NSString *url=[xiugaicardpwd stringByAppendingFormat:@"&user_name=%@&oldPassword=%@&newPassword=%@&cardNum=%@",acc.custPhone,base64oldpwd,base64newpwdstr,self.cardno.text];
                [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
                    

                    
                    
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];
                    
                    //将nsnumber 装换成nsstring
                    NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
                    NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
                    
                    if ([str isEqual:@"2"]) {
                        //返回上一个页面
                        [self.navigationController popViewControllerAnimated:YES];
//                        [self.navigationController popViewControllerAnimated:YES]; //返回上一页面 
                        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"卡密码修改成功"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                        [alert show];
                        
                    }
                    else
                    {
                        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"卡密码修改失败"   message:ary[@"msg"] delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                        [alert show];
                        
                    }
                    
                } faliure:^(NSString *error) {
                    
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];
                    
                }];
                
            }
            
        }
        
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"新密码不能与原密码相同"  message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
    }
    
}






#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}











@end
